﻿//var newStr_1 = '2014-08-6';
//var atrrTime = ['2014 - 09 - 02 - 2014 - 09 - 27', '2014 - 09 - 30 - 2014 - 10- 25'];
//var atrrTimeAll = ['20140805', '20140905', '20140906', '20140907', '20140908', '20140909', '20140910', '20140911', '20140912', '20140913', '20140914', '20140915', '20140916', '20140917', '20140918', '20140919', '20140920', '20140921', '20140922', '20140923', '20140924', '20140925', '20140929', '20140930']
var atrrTimeAll = [];
var newStr_1 = new Date();
var newStr_11 = newStr_1.getFullYear() + '-' + (newStr_1.getMonth() + 1) + '-' + newStr_1.getDate();
var arr_1 = newStr_11.split('-');
var oNowTime = (arr_1[0].length > 1 ? '' : '0') + arr_1[0] + (arr_1[1].length > 1 ? '' : '0') + arr_1[1] + (arr_1[2].length > 1 ? '' : '0') + arr_1[2];
var url = window.location.href;
LiChang = {
    getByClass: function (oPare, cla) {
        var oChild = oPare.getElementsByTagName("*");
        var arr = [];
        for (var i = 0; i < oChild.length; i++) {
            var arrCla = oChild[i].className.split(" ");
            var j;
            for (var j in arrCla) {
                if (arrCla[j] == cla) {
                    arr.push(oChild[i]);
                    break;
                }
            }


        }
        return arr;
    }
}
function showDate(option) {
    this.obj = document.getElementById(option.id);
}
showDate.prototype.init = function () {
    var _self = this;
    _self.dateText = LiChang.getByClass(_self.obj, "showDate")[0];
    _self.dateBox = LiChang.getByClass(_self.obj, "sel_date")[0];
    _self.yearBox = LiChang.getByClass(_self.obj, "year")[0];
    _self.mnBox = LiChang.getByClass(_self.obj, "month")[0];
    _self.dataTable = LiChang.getByClass(_self.dateBox, "data_table")[0];
    _self.tbody = _self.dataTable.tBodies[0];
    _self.td = _self.tbody.getElementsByTagName("td");
    _self.prevM = LiChang.getByClass(_self.dateBox, "prev_m")[0];
    _self.nextM = LiChang.getByClass(_self.dateBox, "next_m")[0];
    _self.prevY = LiChang.getByClass(_self.dateBox, "prev_y")[0];
    _self.nextY = LiChang.getByClass(_self.dateBox, "next_y")[0];
    //显示日历
    _self.dateText.onclick = function () {
        _self.changeDefault(this);
        _self.show();
        sel_date_show();
        //_self.showNow();
        //_self.dateText.blur();
    }
    //点击空白 隐藏日历

    document.onclick = function (event) {
        event = event || window.event;
        var Target = event.target || event.srcElement;
        _self.hide(event, Target, this);


    }
    //点击选择日期
    for (var i = 0; i < _self.td.length; i++) {
        _self.td[i].onclick = function () {
            var newDd = this.innerHTML;

            var newYear = parseInt(_self.yearBox.value, 10);
            var newMn = parseInt(_self.mnBox.value, 10);


            if (newDd.match(/^\s{0}$/g)) {  //如果td没有值;
                //alert(1);
                return false;
            }
            if (isNaN(newYear) || isNaN(newMn) || newYear < 2014 || newYear > 2099 || newMn < 1 || newMn > 12) {  //如果td有值;
                //alert("请填写正确的年和月！");
                return false;
            }
            //选中日期
            var nar = newYear + "-" + newMn + "-" + newDd;
            nar = new Date(Date.parse(nar.replace(/-/g, "/"))).format('yyyy-MM-dd');
            var arr = nar.split('-');
            var newStr = (arr[0].length > 1 ? '' : '0') + arr[0] + (arr[1].length > 1 ? '' : '0') + arr[1] + arr[2];
            //当前日期
            var temp = new Date();
            var fd = temp.format('yyyy-MM-dd');
            var arr1 = fd.split('-');
            var newStr1 = (arr1[0].length > 1 ? '' : '0') + arr1[0] + (arr1[1].length > 1 ? '' : '0') + arr1[1] + arr1[2];
            if (this.innerHTML != "" && newStr >= newStr1 && !getRight(newStr)) {
                $('.active').attr("class", "");
                this.className = "active";
                _self.dateText.innerHTML = nar;
                _self.dateBox.className += " ";
            } else {
                //alert("请填写正确的年和月！");
            }
        }
        _self.td[i].onmouseover = function () {
            if (this.className.indexOf("hove") == -1) {
                this.className += " hover";
            }
        }
        _self.td[i].onmouseout = function () {
            this.className = this.className.replace("hover", "")
        }

    }


    //点击切换月份
    _self.prevM.onclick = _self.nextM.onclick = function () {

        _self.changeMn(this);
        return this;
    }
    //点击切换年份
    _self.prevY.onclick = _self.nextY.onclick = function () {
        _self.changeYr(this);
        return this;
    }
    //年月日文本框手动录入
    _self.yearBox.onkeyup = _self.mnBox.onkeyup = function () {
        this.value = this.value.replace(/\D/g, "");
        var year = parseInt(_self.yearBox.value, 10);
        var month = parseInt(_self.mnBox.value, 10);
        if (!isNaN(year) && year <= 2099 && year >= 2014 && !isNaN(month) && month <= 12 && month >= 1) {
            _self.showAllDay(year, month - 1);
        }
        if (this == _self.yearBox && year >= 2014 && year <= 2099) {
            _self.mnBox.focus();
        } else if (this == _self.mnBox && (month < 1 || month > 12)) {
            if (this.value.slice(0, 1) > 1) { //如果第一位大于1
                this.value = this.value.slice(0, 1);
            } else if (month > 12) { //
                this.value = 12;
            }
            year = parseInt(_self.yearBox.value, 10);
            month = parseInt(_self.mnBox.value, 10);
            _self.showAllDay(year, month - 1);
        }
    }
    _self.yearBox.onblur = function () {
        var year = parseInt(this.value, 10);
        if (year < 2014 || year > 2099 || isNaN(year)) {
            this.focus();
            alert("请输入正确年份！");
            this.value = "";
        }
    }
    _self.mnBox.onfocus = function () {
        var year = parseInt(_self.yearBox.value, 10);
        if (isNaN(year)) {
            _self.yearBox.focus();
        }
    }
    _self.mnBox.onblur = function () {
        var month = parseInt(this.value, 10);
        var year = parseInt(_self.yearBox.value, 10);
        if (month < 1 || month > 12 || isNaN(month)) {
            alert("请输入正确月份！");
            this.value = "";
        }
    }
}
//点击切换月份
showDate.prototype.changeMn = function (obj) {
    var _self = this;
    var NewMn = parseInt(_self.mnBox.value, 10);
    var newYear = parseInt(_self.yearBox.value, 10);
    if (isNaN(NewMn) || isNaN(newYear)) {
        alert("请填写正确的年和月！");
        return false;
    }

    if (obj == _self.nextM) {
        NewMn++;
        if (NewMn == 13) {
            NewMn = 1;
            newYear += 1;
        }
    } else {
        NewMn--;
        if (NewMn == 0) {
            NewMn = 12;
            newYear -= 1;
        }
    }
    _self.mnBox.value = NewMn;
    _self.showNow(newYear, NewMn);
}
//点击切换年份
showDate.prototype.changeYr = function (obj) {
    var _self = this;
    var NewMn = parseInt(_self.mnBox.value, 10);
    var newYear = parseInt(_self.yearBox.value, 10);
    if (isNaN(NewMn) || isNaN(newYear)) {
        alert("请填写正确的年和月！");
        return false;
    }
    if (obj == _self.nextY) {
        newYear++;
    } else {
        newYear--;
    }
    if (newYear < 2014) {
        newYear = 2014;
    } else if (newYear > 2099) {
        newYear = 2099;
    }
    _self.mnBox.value = NewMn;
    _self.showNow(newYear, NewMn);
}
//文本框 清空初始值
showDate.prototype.changeDefault = function (obj) {
    var _self = this;
    var deVal = obj.innerHTML;
    var regExp = /^\s{0,}$/g;
    if (deVal == "请选择月嫂服务时间" || deVal == "选择开始时间" || deVal == "查看月嫂档期") {
        obj.innerHTML = "";
        _self.showNow();
    } else if (deVal.match(regExp) === null && _self.dateBox.className.indexOf("dn") != -1) { //如果显示的是非空字符
        var dayArr = deVal.match(/[0-9]{1,4}/g);
        _self.showNow(dayArr[0], dayArr[1], dayArr[2]); //刷新日期
    }
}
//文本框 还原初始值
showDate.prototype.changeDefault2 = function (obj) {
    var _self = this;
    var deVal = obj.innerHTML;
    if (deVal.match(/^\s{0}$/)) {
        if ($('#data_box .trigger span').length == 2) {
            obj.innerHTML = "选择开始时间";
        } else {
            obj.innerHTML = "请选择月嫂服务时间";
        }

    }
}
//显示日历
showDate.prototype.show = function () {
    var _self = this;
    //dateTop();//日历显示上下位置
    if (_self.dateBox.className.indexOf("dn") != -1) {
        var cls = _self.dateBox.className;
        _self.dateBox.className = cls.replace("dn", "");
    }
}
//隐藏日历
showDate.prototype.hide = function (event, Target, obj) {
    var _self = this;
    var oPare = Target.parentNode;
    var isChild = true; //默认是子元素
    if (oPare == obj || Target == obj) {
        isChild = false;

    } else {
        loop: while (oPare != _self.obj) {
            oPare = oPare.parentNode;
            if (oPare == obj) {
                isChild = false;
                break loop;
            }
        }
    }
    if (!isChild && _self.dateBox.className.indexOf("dn") == -1) {
        sel_date_hide();
        //_self.dateBox.className += " ";
        //_self.dateBox.className += "dn";
        _self.changeDefault2(_self.dateText);
    }
}
//填充年、月
showDate.prototype.showNow = function (yr, mn, date) {

    var _self = this;
    var now = new Date();
    var year = yr || now.getFullYear();
    var month = mn || now.getMonth() + 1;
    var dd = date || now.getDate();

    //填充 年 和 月
    _self.yearBox.value = year;
    _self.mnBox.value = mn || now.getMonth() + 1;
    //填充日期
    _self.showAllDay(year, month - 1, dd);
}
//填充当月的所有日期
showDate.prototype.showAllDay = function (Yr, Mn, Dd) {
    //console.log(Yr + '-' + Mn + '-' + Dd);
    var _self = this;
    var arr31 = [1, 3, 5, 7, 8, 10, 12];
    var is31 = true;
    var newDd = new Date();
    Dd = Dd ? Dd : newDd.getDate();
    newDd.setFullYear(Yr, Mn, Dd); //根据传入的数值生成新的日期
    var year = newDd.getFullYear(),
        month = newDd.getMonth(),
        dd = newDd.getDate();
    var firstD = new Date();
    firstD.setFullYear(year, month, 1);
    var firstDay = firstD.getDay();
    str31 = arr31.join(",");
    var regExp = eval("/" + (month + 1) + ",|," + (month + 1) + ",|," + (month + 1) + "/g");
    var fd = newDd.format('yyyy-MM-dd');
    var arr1 = fd.split('-');
    var newStr = (arr1[0].length > 1 ? '' : '0') + arr1[0] + (arr1[1].length > 1 ? '' : '0') + arr1[1] + arr1[2];
    var dayLen = 31;
    //判断每个月有多少天
    if (month == 1) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            dayLen = 29;
        } else {
            dayLen = 28;
        }
    }
    else if (str31.search(regExp) == -1) {
        dayLen = 30;
    }
    //清空日期
    for (var i = 0; i < _self.td.length; i++) {
        _self.td[i].innerHTML = "";
        _self.td[i].className = "";
    }
    //如果有31天

    for (var j = 0; j < dayLen; j++) {
        _self.td[j + firstDay].innerHTML = j + 1;
        var date = j + 1;
        if (date < 10) { date = '0' + date }
        var newStr = (arr1[0].length > 1 ? '' : '0') + arr1[0] + (arr1[1].length > 1 ? '' : '0') + arr1[1] + date;
        newStr = parseInt(newStr);
        oNowTime = parseInt(oNowTime);
        if (getRight(newStr)) {
            _self.td[j + firstDay].className = "noSchedule";
        } else {
            if (oNowTime > newStr) {
                _self.td[j + firstDay].className = "overdue";
                //} else if (oNowTime == newStr && j + 1 == dd && _self.td[j + firstDay].className.indexOf("active") != -1) {
                //     _self.td[j + firstDay].className = "active";
            } else if (j + 1 == dd && _self.td[j + firstDay].className.indexOf("active") == -1) {
                _self.td[j + firstDay].className = "active";
            } else {
                _self.td[j + firstDay].className = "";
            }
        }
    }
}
//函数调用
window.onload = function () {
    var showDate1 = new showDate({ id: "data_box" });
    showDate1.init();
}


Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

var addNDays = function (date, n) {
    var d = new Date(Date.parse(date.replace(/-/g, "/")));
    var time = d.getTime();
    var newTime = time + (n - 1) * 24 * 60 * 60 * 1000;
    return new Date(newTime);
};
function dateDiff(date1, date2) {
    var type1 = typeof date1, type2 = typeof date2;
    if (type1 == 'string')
        date1 = stringToTime(date1);
    else if (date1.getTime)
        date1 = date1.getTime();
    if (type2 == 'string')
        date2 = stringToTime(date2);
    else if (date2.getTime)
        date2 = date2.getTime();

    return (date2 - date1) / (1000 * 60 * 60 * 24); //结果是秒
}
function getURLParameter(oName) {
    return decodeURI((RegExp(oName + '=' + '(.+?)(&|$|#)').exec(location.href) || [, null])[1]);
}
(function () {
    if (getURLParameter('shsj').indexOf('-') != -1) {
        var arr1 = getURLParameter('shsj').split('-');
        var a = arr1[0] + '-' + arr1[1] + '-' + arr1[2];
        var b = arr1[3] + '-' + arr1[4] + '-' + arr1[5];
        $(".showDate").html(a + '至' + b);
        //$(".showDateEnd").html(b);
        //var day = 1 * ((((new Date(b).getTime() - new Date(a).getTime()) / (1000 * 60 * 60 * 24) + 1) / 26));
        //selectmonth(day);
    }
})();
//日历显示上下位置
function dateTop() {
    var oHeight = $('#data_box').offset().top;
    var scrollTop = document.body.scrollTop && document.documentElement.scrollTop; //页面滚动条距离
    oScroll = $(document).scrollTop();
    if (oHeight - oScroll < 350) {
        $('.sel_date').css({ 'bottom': 'auto', 'top': '29px' });
    } else {
        $('.sel_date').css({ 'bottom': '36px', 'top': 'auto' });
    }
}
//月嫂档期已占
function getRight(num) {
    var oBtn = false,
        len = atrrTimeAll.length;
    for (var i = 0; i < len; i++) {
        var oData = atrrTimeAll[i];
        if (num == oData) {
            oBtn = true;
        }
    }
    return oBtn;
}